/* 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

Title : com.Angaliwa.Griwk
Author : Dave Jansen -- Zenitum
URL : http://www.zenitum.com/

Description : This module allows you to easily set the size
			  and position of objects that are not possible
			  otherwise, from percentage-based sizes to 
			  making items 'stick' to a specific side. 
			  Requires Angaliwa's StageHandler, DaisyChain
			  and Settings modules (or equivalent). 

History		: v1.0.090827 Initial version

Created 	: August 27th, 2009

Version		: 1.0
Compatibility: 1.0

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
*/

package com.Angaliwa{
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.containers.Canvas;
	import mx.controls.Text;
	import mx.core.Container;
	import mx.effects.Fade;
	import mx.effects.easing.Quartic;
	import mx.events.EffectEvent;
	
	public class Growl{
		public var settings:Settings = new Settings(this),
				   daisyChain:DaisyChain = new DaisyChain(this),
				   stageHandler:StageHandler = Core.g('stageHandler'),
				   resizeHandler:ResizeHandler = Core.g('resizeHandler'),
				   container:Canvas = new Canvas();
		
		private var shown:Array = [],
					layer:String;
		
		public function init(layer:Canvas, customSettings:Object=undefined):Growl{
			layer.addChild(container);
			
			resizeHandler.setSize(container, '80%', '100%-40px')
						 .setPosition(container,'center','top+20');
			
			settings.setDefaults({
						showTimeout: 4000,
						aniIn: {
							duration: 500,
							easing: Quartic.easeIn
						},
						aniOut: {
							duration: 2000,
							easing: Quartic.easeOut
						},
						containerStyles: {
							backgroundColor: 0x000000,
							backgroundAlpha: 0.7,
							cornerRadius: 5,
							borderThickness: 0,
							borderStyle: 'solid'
						},
						textStyles: {
							color: 0xffffff,
							align: 'center'
						}
					});
			
			if(customSettings)
				settings.add(customSettings);
			
			
			// Debug
			message('Hello everybody! Say hi to Angaliwa\'s first Growl message!');
			
			return this;
		}
		
		public function message(text:String):void{
			// DEBUG
			create(text);
		}
		
		public function create(text:String="Hello world"):Container{
			// Initialize the default style -- DEBUG
			var style:Container = new Container();
			style.width = 600;
			style.height = 24;
			style.alpha = 0;
			style.y = (shown.length*30);
			
			// Apply container styles
			Toolbox.applyStyles(style,settings.get('containerStyles'));
			
			var txt:Text = new Text();
			txt.width = 580;
			txt.height = 20;
			txt.x = 20;
			txt.y = 2;
			txt.selectable=false;
			
			// Apply container styles
			Toolbox.applyStyles(txt,settings.get('textStyles'));
			
			txt.text = text;
			
			style.addChild(txt);
			shown.push(style);
			style.id = String(shown.length+1);
			
			// Prepare animation effects
			var aniIn:Fade = new Fade(style);
			aniIn.alphaFrom = 0;
			aniIn.alphaTo=1;
			aniIn.easingFunction = settings.get('aniIn').easing;
			aniIn.duration = settings.get('aniIn').duration;
			aniIn.addEventListener(EffectEvent.EFFECT_END, function():void{
				var timer:Timer = new Timer(settings.get('showTimeout'),1);
				timer.addEventListener(TimerEvent.TIMER_COMPLETE, function():void{
					aniOut.play();
				});
				timer.start();
			});
			
			var aniOut:Fade = new Fade(style);
			aniOut.alphaFrom = 1;
			aniOut.alphaTo=0;
			aniOut.easingFunction = settings.get('aniOut').easing;
			aniOut.duration = settings.get('aniOut').duration;
			aniOut.addEventListener(EffectEvent.EFFECT_END, function():void{
				container.removeChild(style);
				shown.shift();
			});
			style.addEventListener(MouseEvent.CLICK, function():void{
				aniOut.play();
			});
			
			container.addChild(style);
			
			resizeHandler.setSize(txt, '100%-20px', '20px')
						 //.setPosition(txt,'center','2px')
						 .setSize(style, '100%', '24px');
						 //.setPosition(style,'center','20px');
			
			// Start the fade-in animation
			aniIn.play();
			
			// DEBUG
			trace('[GROWL] [create] Showing message: '+text);
			
			return style;
		}
	}
}